package com.maidada.codesharebackend.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.maidada.codesharebackend.common.BaseRequest;
import com.maidada.codesharebackend.common.CommonResult;
import com.maidada.codesharebackend.common.PageResult;
import com.maidada.codesharebackend.constant.UserRoleConstant;
import com.maidada.codesharebackend.model.dto.tag.TagAddRequest;
import com.maidada.codesharebackend.model.dto.tag.TagQueryRequest;
import com.maidada.codesharebackend.model.dto.tag.TagUpdateRequest;
import com.maidada.codesharebackend.model.vo.TagVO;
import com.maidada.codesharebackend.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 标签表 Controller
 *
 * @author wulinxuan
 */
@Slf4j
@RestController
@RequestMapping("/tag")
public class TagController {

    @Resource
    private TagService tagService;

    /**
     * 新增标签
     *
     * @param tagAddRequest 新增请求
     * @return 标签id
     */
    @SaCheckRole(UserRoleConstant.ADMIN)
    @PostMapping("/add")
    public CommonResult<Long> addTag(@Valid @RequestBody TagAddRequest tagAddRequest) {
        Long tagId = tagService.addTag(tagAddRequest);
        return CommonResult.success(tagId);
    }

    /**
     * 删除标签
     *
     * @param baseRequest 请求参数
     * @return 是否成功
     */
    @SaCheckRole(UserRoleConstant.ADMIN)
    @PostMapping("/delete")
    public CommonResult<Boolean> deleteTag(@Valid @RequestBody BaseRequest baseRequest) {
        Boolean result = tagService.deleteTag(baseRequest.getId());
        return CommonResult.success(result);
    }

    /**
     * 修改标签
     *
     * @param tagUpdateRequest 修改请求
     * @return 是否成功
     */
    @SaCheckRole(UserRoleConstant.ADMIN)
    @PostMapping("/update")
    public CommonResult<Boolean> updateTag(@Valid @RequestBody TagUpdateRequest tagUpdateRequest) {
        Boolean result = tagService.updateTag(tagUpdateRequest);
        return CommonResult.success(result);
    }

    /**
     * 分页查询标签
     *
     * @param tagQueryRequest 查询请求
     * @return 分页结果
     */
    @GetMapping("/page")
    public CommonResult<PageResult<TagVO>> pageTag(@Valid TagQueryRequest tagQueryRequest) {
        PageResult<TagVO> pageResult = tagService.pageTag(tagQueryRequest);
        return CommonResult.success(pageResult);
    }

    /**
     * 获取标签详情
     *
     * @param id 标签id
     * @return 标签信息
     */
    @GetMapping("/get")
    public CommonResult<TagVO> getTagById(@RequestParam Long id) {
        TagVO tagVO = tagService.getTagById(id);
        return CommonResult.success(tagVO);
    }
}

